home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / graph_layout / mswindow.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  7.8 KB  |  266 lines

  1. /***************************************************************************
  2. **    TEST FILE FOR graph (Dynamic Layout Alg)
  3. **
  4. **    MODUL: class library to interface MS-WINDOWS
  5. **
  6. ** Author: dr. Szirmay-Kalos Laszlo (szirmay@fsz.bme.hu)
  7. **       Technical University of Budapest, Hungary
  8. *****************************************************************************/
  9. #include "mswindow.hxx"
  10.  
  11. static AppWindow * pwindow;
  12.  
  13. long   FAR PASCAL _export WndProc( unsigned int, unsigned int, unsigned int, long );
  14.  
  15.  
  16. //-------------------------------------------------------------------
  17. void App :: Error( char * message, int line )
  18. //-------------------------------------------------------------------
  19. {
  20.     fprintf( stderr, "ERROR: %s", message );
  21.     if ( line >= 0 ) fprintf( stderr, " in line %d", line );
  22.     fprintf( stderr, "\n" );
  23.     Quit( );
  24. }
  25.  
  26. //-------------------------------------------------------------------
  27. void App :: Warning( char * message )
  28. //-------------------------------------------------------------------
  29. {
  30.     fprintf( stderr, "ERROR: %s\n", message );
  31. }
  32.  
  33. //-------------------------------------------------------------------
  34. void App :: Quit( )
  35. //-------------------------------------------------------------------
  36. {
  37.     fprintf( stderr, "Bye ( Graph )\n" );
  38.     exit( -1 );
  39. }
  40.  
  41. static HANDLE hInstance;
  42. static HANDLE hPrevInstance;
  43. static int    nCmdShow;
  44.  
  45. //-------------------------------------------------------------------
  46. AppWindow ::  AppWindow( int argc, char * argv[] )
  47. //-------------------------------------------------------------------
  48.        : canvas( 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT )
  49. {
  50.     WNDCLASS wndclass;
  51.  
  52.     pwindow = this;
  53.  
  54.     strcpy(szClassName, "GRAPH");
  55.  
  56.     if ( ! hPrevInstance ) {
  57.         wndclass.style           = CS_HREDRAW | CS_VREDRAW;
  58.         wndclass.lpfnWndProc   = ::WndProc;
  59.         wndclass.cbClsExtra    = 0;
  60.         wndclass.cbWndExtra    = 0;
  61.         wndclass.hInstance     = hInstance;
  62.         wndclass.hIcon           = LoadIcon( hInstance, IDI_APPLICATION );
  63.         wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
  64.         wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
  65.         wndclass.lpszMenuName  = NULL;
  66.         wndclass.lpszClassName = szClassName;
  67.  
  68.         if ( ! RegisterClass( &wndclass ) )   exit( -1 );
  69.     }
  70.  
  71.     hWnd = CreateWindow( szClassName,        // window class name
  72.                  "Graph layout",        // window caption
  73.                  WS_OVERLAPPEDWINDOW,    // window style
  74.                  CW_USEDEFAULT,        // initial x pos
  75.                  CW_USEDEFAULT,        // initial y pos
  76.                  CW_USEDEFAULT,        // initial x size
  77.                  CW_USEDEFAULT,        // initial x size
  78.                  NULL,            // parent window handle
  79.                  NULL,            // window menu handle
  80.                  hInstance,            // program instance handle
  81.                  NULL );            // creation params
  82.  
  83.     if ( ! hWnd ) exit( -1 );
  84.     ShowWindow( hWnd, nCmdShow );
  85. }
  86.  
  87.  
  88. //-------------------------------------------------------------------
  89. RectAngle AppWindow :: Canvas()
  90. //-------------------------------------------------------------------
  91. {
  92.     return canvas;
  93. }
  94.  
  95. //-------------------------------------------------------------------
  96. void AppWindow :: RePaint()
  97. //-------------------------------------------------------------------
  98. {
  99.     InvalidateRect( hWnd, NULL, TRUE );  // WM_PAINT message
  100. }
  101.  
  102. //-------------------------------------------------------------------
  103. void AppWindow :: Text( char * text, Point p)
  104. //-------------------------------------------------------------------
  105. {
  106.     TEXTMETRIC tm;
  107.     GetTextMetrics( hdc, &tm );
  108.  
  109.     TextOut(hdc,
  110.         p.X() - tm.tmMaxCharWidth / 2,
  111.         p.Y() - tm.tmHeight / 2,
  112.         text, strlen( text ));
  113. }
  114.  
  115. //-------------------------------------------------------------------
  116. void AppWindow :: MoveTo( Point p )
  117. //-------------------------------------------------------------------
  118. {
  119.     ::MoveTo(hdc, p.X(), p.Y() );
  120. }
  121.  
  122. //-------------------------------------------------------------------
  123. void AppWindow :: LineTo( Point p )
  124. //-------------------------------------------------------------------
  125. {
  126.     ::LineTo(hdc, p.X(), p.Y());
  127. }
  128.  
  129. //-------------------------------------------------------------------
  130. void AppWindow :: DrawRectangle( RectAngle& rect )
  131. //-------------------------------------------------------------------
  132. {
  133.     RECT r;
  134.     r.left = rect.HorPos();
  135.     r.top = rect.VerPos();
  136.     r.right = r.left + rect.Width();
  137.     r.bottom = r.top + rect.Height();
  138.     FillRect( hdc, &r, GetStockObject( BLACK_BRUSH ) );
  139. }
  140.  
  141. //-------------------------------------------------------------------
  142. void AppWindow :: MessageLoop()
  143. //-------------------------------------------------------------------
  144. {
  145.     MSG msg;
  146.  
  147.     while( GetMessage( &msg, NULL, 0, 0 ) ) {
  148.         TranslateMessage( &msg );
  149.         DispatchMessage( &msg );
  150.     }
  151. }
  152.  
  153. //-------------------------------------------------------------------
  154. // FORWARD EVENT TO EVENT HANDLERS
  155. //
  156. long AppWindow::WindowProc( HWND hwnd, WORD wmsg, WORD wParam, LONG lParam )
  157. //-------------------------------------------------------------------
  158. {
  159.     hWnd = hwnd;
  160.  
  161.     switch ( wmsg ) {
  162. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  163.     case WM_PAINT: {                // REPAINT WINDOW
  164. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165.             RECT    rect;
  166.             GetClientRect( hWnd, &rect );
  167.             canvas = RectAngle( rect.left, rect.top,
  168.                         rect.right - rect.left,
  169.                         rect.bottom - rect.top );
  170.             ExposeEvent evt( &canvas );
  171.             hdc = BeginPaint(hWnd, &ps);
  172.             ExposeAll( &evt );
  173.             EndPaint( hWnd, &ps );
  174.         }
  175.         break;
  176.  
  177. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  178.     case WM_CHAR: {                    // KEYBOARD EVENT
  179. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  180.             KeyEvent evt(wParam, lParam);
  181.             hdc = GetDC( hWnd );
  182.             KeyPressed( &evt );
  183.             ReleaseDC( hWnd, hdc );
  184.         }
  185.         break;
  186. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  187.     case WM_DESTROY:                // CLOSE WINDOW
  188. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  189.         PostQuitMessage( 0 );
  190.         break;
  191.  
  192. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  193.     default:                        // ALL OTHER EVENTS
  194. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  195.         return DefWindowProc( hwnd, wmsg, wParam, lParam );
  196.     }
  197.  
  198.     return 0;
  199. }
  200.  
  201. /********************************************************************
  202. *                                    *
  203. *  MS-WINDOWS INTERFACE                            *
  204. ********************************************************************/
  205. //-------------------------------------------------------------------
  206. // EVENT HANDLER, CALLED BY WINDOWS
  207. //
  208. long FAR PASCAL _export WndProc( unsigned int hWnd,
  209.                  unsigned int message,
  210.                  unsigned int wParam,
  211.                  long lParam )
  212. //-------------------------------------------------------------------
  213. {
  214.     return pwindow->WindowProc( hWnd, message, wParam, lParam );
  215. }
  216.  
  217. //-------------------------------------------------------------------
  218. //    APPLICATION OBJECT
  219. //-------------------------------------------------------------------
  220. App app;
  221.  
  222. //-------------------------------------------------------------------
  223. // PROGRAM ENTRY POINT, CALLED BY WINDOWS
  224. //
  225. int PASCAL WinMain( HANDLE hInstance,
  226.             HANDLE hPrevInstance,
  227.             LPSTR lpszCmdLine,
  228.             int nCmdShow )
  229. //-------------------------------------------------------------------
  230. {
  231.     ::hInstance = hInstance;
  232.     ::hPrevInstance = hPrevInstance;
  233.     ::nCmdShow = nCmdShow;
  234.  
  235. /*
  236. *   MAKE argc, argv FROM lpsCmdLine
  237. */
  238.     char * argv[20];
  239.     argv[0] = new char[ strlen( "graph" ) + 1 ];
  240.     strcpy( argv[0], "graph" );
  241.  
  242.     int argc = 1;
  243.     char far * start = lpszCmdLine;
  244.     for( char far * ps = lpszCmdLine; *ps != '\0'; ps++ ) {
  245.         if ( *ps != ' ' && *ps != '\t' ) continue;
  246.         else if ( ps != start ) {
  247.             argv[ argc ] = ( char * ) malloc( ps - start + 1);
  248.             for(int i = 0; i < ps - start; i++ )
  249.                 argv[argc][i] = start[i];
  250.             argv[argc++][i] = '\0';
  251.         }
  252.  
  253.     }
  254.     if ( ps != start ) {
  255.         argv[ argc ] = ( char * )malloc( ps - start + 1);
  256.         for(int i = 0; i < ps - start; i++ )
  257.             argv[argc][i] = start[i];
  258.         argv[argc++][i] = '\0';
  259.     }
  260. /*
  261. *   CALL APPLICATION DEPENDENT ENTRY
  262. */
  263.     app.Start( argc, argv );
  264.     return 0;
  265. }
  266.